/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package olympicgamesgui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.Enumeration;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import olympicgameslogic.*;
import org.jfree.ui.RefineryUtilities;

/**
 *
 * @author fabio mendonca
 */
public class DialogCompAnalysis extends JFrame {

   private LinkedList<Nation> nts = OlympicGamesGui.ap.getNations();
   private JComboBox comboNation1, comboNation2, comboNation3, comboNation4, comboNation5;
   private JPanel psul;
   private ImageIcon img;
   private JTable tabela1;
   ClassLoader cldr = this.getClass().getClassLoader();
   private DefaultTableModel TableModel;
   private Object[][] data1;
   private Object[] columnNames = {"Nation", "Editions\nEntered", "Disciplines\nwith participants", "Medals Won\n(G/S/B)", "Overall\nRanking", "Medals per\nEdition(Avg)"};
   MultiLineChart graf;

   public DialogCompAnalysis() {
      super("Comparison Analysis");
      super.setIconImage(Toolkit.getDefaultToolkit().getImage(cldr.getResource("images/extra/icon.png")));
   }

   public void showJan() {


//aqui vamos definir qual o ranking que será aberto dependendo do que e passado como atributo           
      setLayout(new BorderLayout());


//    Criação da barra de topo
      JPanel pannorte = new JPanel(new FlowLayout(FlowLayout.CENTER));
      pannorte.setSize(200, 60);
      pannorte.setBackground(Color.white);
      img = new ImageIcon(cldr.getResource("images/extra/chart.png"));
      JLabel background = new JLabel("Comparison Analysis", JLabel.CENTER);
      background.setFont(new Font("Arial", Font.BOLD, 14));
      background.setIcon(img);

      pannorte.add(background);

      JPanel pcentro = new JPanel(new BorderLayout());
      JPanel p1 = new JPanel(new GridLayout(6, 1));
      JLabel lb = new JLabel("Choose Countries to Compare");
      lb.setFont(new Font("Aria", Font.PLAIN, 12));
//      p1.add(lb,CENTER_ALIGNMENT);

////combobox 1 para escoler o 1 pais  

      comboNation1 = new JComboBox(nts.toArray());
      comboNation1.setMaximumSize(new Dimension(70, 20));
      comboNation1.setSelectedIndex(-1);
      comboNation1.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    JComboBox box = (JComboBox) (e.getSource());
	    if (box.getSelectedIndex() > -1) {
	       comboNation2.removeAllItems();
	       comboNation2.setModel(new DefaultComboBoxModel(nts.toArray()));
	       comboNation2.removeItem(box.getSelectedItem());
	       comboNation2.setEnabled(true);
	       comboNation2.setSelectedIndex(-1);
	       comboNation3.setSelectedIndex(-1);
	       comboNation3.setEnabled(false);
	       comboNation4.setSelectedIndex(-1);
	       comboNation4.setEnabled(false);
	       comboNation5.setSelectedIndex(-1);
	       comboNation5.setEnabled(false);
	    }
	 }
      });
      p1.add(comboNation1);

//////combobox 2 para escoler o 1 pais      
      comboNation2 = new JComboBox(nts.toArray());
      comboNation2.setMaximumSize(new Dimension(70, 20));
      comboNation2.setEnabled(false);
      comboNation2.setSelectedIndex(-1);
      comboNation2.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    JComboBox box = (JComboBox) (e.getSource());
	    if (box.getSelectedIndex() > -1) {
	       comboNation3.removeAllItems();
	       comboNation3.setModel(new DefaultComboBoxModel(nts.toArray()));
	       comboNation3.removeItem(box.getSelectedItem());
	       comboNation3.removeItem(comboNation1.getSelectedItem());
	       comboNation3.setEnabled(true);
	       comboNation3.setSelectedIndex(-1);
	       comboNation4.setSelectedIndex(-1);
	       comboNation4.setEnabled(false);
	       comboNation5.setSelectedIndex(-1);
	       comboNation5.setEnabled(false);

	    }

	 }
      });
      p1.add(comboNation2);

//////combobox 3 para escoler o 1 pais      
      comboNation3 = new JComboBox();
      comboNation3.setMaximumSize(new Dimension(70, 20));// 
      comboNation3.setSelectedIndex(-1);
      comboNation3.setEnabled(false);
      comboNation3.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    JComboBox box = (JComboBox) (e.getSource());
	    if (box.getSelectedIndex() > -1) {
	       comboNation4.removeAllItems();
	       comboNation4.setModel(new DefaultComboBoxModel(nts.toArray()));
	       comboNation4.removeItem(box.getSelectedItem());
	       comboNation4.removeItem(comboNation1.getSelectedItem());
	       comboNation4.removeItem(comboNation2.getSelectedItem());
	       comboNation4.setEnabled(true);
	       comboNation4.setSelectedIndex(-1);
	       comboNation5.setEnabled(true);
	       comboNation5.setSelectedIndex(-1);

	    }
	 }
      });
      p1.add(comboNation3);

//////combobox 4 para escoler o 1 pais      
      comboNation4 = new JComboBox();
      comboNation4.setMaximumSize(new Dimension(70, 20));
      comboNation4.setSelectedIndex(-1);
      comboNation4.setEnabled(false);
      comboNation4.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    JComboBox box = (JComboBox) (e.getSource());
	    if (box.getSelectedIndex() > -1) {
	       comboNation5.removeAllItems();
	       comboNation5.setModel(new DefaultComboBoxModel(nts.toArray()));
	       comboNation5.removeItem(box.getSelectedItem());
	       comboNation5.removeItem(comboNation1.getSelectedItem());
	       comboNation5.removeItem(comboNation2.getSelectedItem());
	       comboNation5.removeItem(comboNation3.getSelectedItem());
	       comboNation5.setEnabled(true);
	       comboNation5.setSelectedIndex(-1);

	    }

	 }
      });
      p1.add(comboNation4);

//////combobox 5 para escoler o 1 pais      
      comboNation5 = new JComboBox();
      comboNation5.setMaximumSize(new Dimension(70, 20));
      comboNation5.setSelectedIndex(-1);
      comboNation5.setEnabled(false);
      p1.add(comboNation5);
//      
      pcentro.add(p1, BorderLayout.WEST);

      TableModel = new DefaultTableModel(data1, columnNames);
      TableModel.setColumnIdentifiers(columnNames);
      tabela1 = new JTable() {

	 public boolean isCellEditable(int rowIndex, int colIndex) {
	    return false; //Disallow the editing of any cell
	 }

	 @Override
	 public Class getColumnClass(int column) {
	    if (column == 0) {
	       return ImageIcon.class;
	    }
	    return Object.class;
	    //return getValueAt(0, column).getClass();
	 }
      };
      setUpTableData();
      tabela1.setPreferredScrollableViewportSize(new Dimension(300, 70));
      tabela1.setFillsViewportHeight(true);
      tabela1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      tabela1.getTableHeader().setReorderingAllowed(false);
      tabela1.getTableHeader().setResizingAllowed(false);
      JScrollPane ptab = new JScrollPane(tabela1);

      pcentro.add(ptab, BorderLayout.CENTER);


//Painel SUL      
      psul = new JPanel(new FlowLayout(FlowLayout.CENTER));

      JButton buttonGraf = new JButton("Graphic");
      buttonGraf.setFont(new Font("Arial", Font.PLAIN, 8));
      buttonGraf.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    if (graf != null) {
	       graf.dispose();
	    }
	    LinkedList<Nation> nationsGraph = new LinkedList<>();

	    if (comboNation1.getSelectedIndex() > -1) {
	       nationsGraph.add((Nation) comboNation1.getSelectedItem());
	    }
	    if (comboNation2.getSelectedIndex() > -1) {
	       nationsGraph.add((Nation) comboNation2.getSelectedItem());//  nationsGraph.add((Nation) comboNation2.getSelectedItem());
	    }
	    if (comboNation3.getSelectedIndex() > -1) {
	       nationsGraph.add((Nation) comboNation3.getSelectedItem());
	    }
	    if (comboNation4.getSelectedIndex() > -1) {
	       nationsGraph.add((Nation) comboNation4.getSelectedItem());
	    }
	    if (comboNation5.getSelectedIndex() > -1) {
	       nationsGraph.add((Nation) comboNation5.getSelectedItem());
	    }
	    graf = new MultiLineChart("Multi Line Comparisson Chart", nationsGraph);
	    RefineryUtilities.centerFrameOnScreen(graf);

	 }
      });
      JButton btrefresh = new JButton("Refresh");
      btrefresh.setFont(new Font("Arial", Font.PLAIN, 8));
      btrefresh.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    refreshTable();
	 }
      });
      psul.add(buttonGraf);
      psul.add(btrefresh);
      p1.add(psul);

//Desenho      
      add(pannorte, BorderLayout.NORTH);
      add(pcentro, BorderLayout.CENTER);
      pack();
      setSize(802, 240);
      setResizable(false);
      setLocationRelativeTo(null);
      setBackground(Color.white);
      setVisible(true);
      setDefaultCloseOperation(DISPOSE_ON_CLOSE);
   }

   private void setUpTableData() {

      LinkedList<Nation> nations = OlympicGamesGui.ap.getNations();
      System.out.println("teste");
      LinkedList<Nation> nationList = new LinkedList<>();
      TableModel.setColumnIdentifiers(columnNames);

      if (comboNation1.getSelectedIndex() > -1) {
	 nationList.add((Nation) comboNation1.getSelectedItem());
      }
      if (comboNation2.getSelectedIndex() > -1) {
	 nationList.add((Nation) comboNation2.getSelectedItem());
      }
      if (comboNation3.getSelectedIndex() > -1) {
	 nationList.add((Nation) comboNation3.getSelectedItem());
      }
      if (comboNation4.getSelectedIndex() > -1) {
	 nationList.add((Nation) comboNation4.getSelectedItem());
      }
      if (comboNation5.getSelectedIndex() > -1) {
	 nationList.add((Nation) comboNation5.getSelectedItem());
      }
      Collections.sort(nations, new Nation.MedalsAbsoluteComparator());

      data1 = new Object[nationList.size() + 1][columnNames.length];


      int i = 0;
      for (Nation nation : nationList) {
	 String img = "images/flags/" + nation.getCode() + ".png";
	 data1[i][0] = new ImageIcon(cldr.getResource(img));
	 data1[i][1] = OlympicGamesGui.ap.getGamesEntered(nation);
	 data1[i][2] = OlympicGamesGui.ap.getTotalEventsParticipation(nation);
	 data1[i][3] = nation.getMedals(Medal.MEDALTYPE.GOLD) + "/" + nation.getMedals(Medal.MEDALTYPE.SILVER) + "/" + nation.getMedals(Medal.MEDALTYPE.BRONZE);
	 data1[i][4] = nations.indexOf(nation) + 1;
	 data1[i][5] = (OlympicGamesGui.ap.getGamesEntered(nation) > 0 ? (float) (nation.getTotalMedals() / OlympicGamesGui.ap.getGamesEntered(nation))%.2f : 0);
	 TableModel.addRow(data1[i]);
	 i++;


      }
      tabela1.setModel(TableModel);
      CustomHeaderRenderer customHeaderRenderer = new CustomHeaderRenderer();
      Enumeration e = tabela1.getColumnModel().getColumns();
      while (e.hasMoreElements()) {
	 ((TableColumn) e.nextElement()).setHeaderRenderer(customHeaderRenderer);
      }
      DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
      rightRenderer.setHorizontalAlignment(JLabel.CENTER);
      tabela1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      tabela1.setAlignmentY(CENTER_ALIGNMENT);
      TableColumn col = tabela1.getColumnModel().getColumn(0);
      col.setPreferredWidth(50);
      col = tabela1.getColumnModel().getColumn(1);
      col.setPreferredWidth(80);
      col.setCellRenderer(rightRenderer);
      col = tabela1.getColumnModel().getColumn(2);
      col.setPreferredWidth(100);
      col.setCellRenderer(rightRenderer);
      col = tabela1.getColumnModel().getColumn(3);
      col.setPreferredWidth(80);
      col.setCellRenderer(rightRenderer);
      col = tabela1.getColumnModel().getColumn(4);
      col.setPreferredWidth(80);
      col.setCellRenderer(rightRenderer);
      col = tabela1.getColumnModel().getColumn(5);
      col.setPreferredWidth(80);
      col.setCellRenderer(rightRenderer);
   }

   public void refreshTable() {
      TableModel.setRowCount(0);
      tabela1.removeAll();
      setUpTableData();
   }

   class CustomHeaderRenderer extends JTextPane implements TableCellRenderer {

      public CustomHeaderRenderer() {
	 setOpaque(true);
	 setForeground(UIManager.getColor("TableHeader.foreground"));
	 setBackground(UIManager.getColor("TableHeader.background"));
	 setBorder(UIManager.getBorder("TableHeader.cellBorder"));
	 StyledDocument doc = this.getStyledDocument();
	 MutableAttributeSet standard = new SimpleAttributeSet();
	 StyleConstants.setAlignment(standard, StyleConstants.ALIGN_CENTER);
	 StyleConstants.setFontFamily(standard, "Arial");
	 StyleConstants.setBold(standard, true);
	 doc.setParagraphAttributes(0, 0, standard, true);
      }

      @Override
      public Component getTableCellRendererComponent(JTable jTable,
	      Object obj, boolean isSelected, boolean hasFocus, int row,
	      int column) {
	 setText((String) obj);
	 return this;
      }
   }
}
